package com.hup.utils.commons;

import com.hup.utils.commons.interfaces.Matchers.Matcher1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author hugan
 * @date 2019/8/9
 */
public class ListUtil {

    public static <T> void iterator(T[] array, BaseIterator<T> iterator) {
        int length = array.length;
        for (int i = 0; i < length; i++) {
            boolean notBreak = iterator.each(i, array[i]);
            if (!notBreak) {
                break;
            }
        }
    }

    /**
     * 适合需要多次get元素的
     * @param size list的size
     * @param targetIndex 目标元素index
     * @return nullable-index越界或get(i)==null
     */
    public static <T> T get(List<T> list, int size, int targetIndex) {
        if (targetIndex < size) {
            return list.get(targetIndex);
        }
        return null;
    }

    public static char[] merge(char[] g1, char[] g2) {
        char[] res = new char[g1.length + g2.length];
        System.arraycopy(g1, 0, res, 0, g1.length);
        System.arraycopy(g2, 0, res, g1.length, g2.length);
        return res;
    }

    public static <T> T get(T[] arr, Matcher1<T> matcher1) {
        for (T item : arr) {
            if (matcher1.isMatch(item)) {
                return item;
            }
        }
        return null;
    }

    public static <T> T get(List<T> list, Matcher1<T> matcher1) {
        for (T item : list) {
            if (matcher1.isMatch(item)) {
                return item;
            }
        }
        return null;
    }

    public static <T> ArrayList<T> getAll(List<T> list, Matcher1<T> matcher1) {
        ArrayList<T> res = new ArrayList<>();
        for (T item : list) {
            if (matcher1.isMatch(item)) {
                res.add(item);
            }
        }
        return res;
    }

    public static <T> int indexOf(List<T> list, Matcher1<T> matcher1) {
        int i = 0;
        for (T obj : list) {
            if (matcher1.isMatch(obj)) {
                return i;
            }
            i++;
        }
        return -1;
    }

    public static <T> boolean contains(List<T> list, Matcher1<T> matcher1) {
        return indexOf(list, matcher1) != -1;
    }

    public static boolean[] booleans(int size, boolean defVal) {
        boolean[] res = new boolean[size];
        if (defVal) {
            for (int i = 0; i < size; i++) {
                res[i] = true;
            }
        }
        return res;
    }

    public static int count(boolean[] arr, boolean tarVal) {
        int count = 0;
        for (boolean b : arr) {
            if (b == tarVal) {
                count++;
            }
        }
        return count;
    }

    public static Integer getSize(List list) {
        return list == null ? null : list.size();
    }

    public static <T> ArrayList<T> subList(ArrayList<T> list, int fromIndex, int toIndex) {
        return new ArrayList<>(list.subList(fromIndex, toIndex));
    }

    public static boolean nullOrEmpty(List list) {
        return list == null || list.isEmpty();
    }

    /**
     * 解决 {@link Arrays#asList(Object[])}返回的list不是 {@link ArrayList}
     */
    public static <T> ArrayList<T> asList(T[] array) {
        ArrayList<T> res = new ArrayList<>(array.length);
        Collections.addAll(res, array);
        return res;
    }

    public static <T> void swap(ArrayList<T> list, int i1, int i2) {
        T temp = list.get(i1);
        list.set(i1, list.get(i2));
        list.set(i2, temp);
    }

    public interface BaseIterator<T> {
        /**
         * @return notBreak
         */
        boolean each(int index, T item);
    }

}
